<?php
date_default_timezone_set("UTC");
include_once("../smiley.php");

class UsersInfo{
    public $first_name="";
    public $last_name="";
    public $display_name="";
    public $primary_email="";
    public $recovery_email="";
    public $current_password="";
    public $new_password="";
    public $user_location="";
    public $user_address="";
    public $user_phone_number="";
    public $user_dob="";
    public $last_logged_in_location="";
    
    function __construct(){
        $this->first_name="";
        $this->last_name="";
        $this->display_name="";
        $this->primary_email="";
        $this->recovery_email="";
        $this->current_password="";
        $this->new_password="";
        $this->user_location="";
        $this->user_address="";
        $this->user_phone_number="";
        $this->user_dob="";
        $this->last_logged_in_location="";  
    }
    
}
class CRUDOperations extends PostAction{
    
    //public $postAction;
    private $loggedInUserId=0;
    public $pdodbobj;
    public $connstatus=false;
    public $lastInsertedID=0;
    function __construct() {
       try{
            $this->pdodbobj= new DB();
            $this->connstatus=$this->pdodbobj->connect();
            
            if(isset($_SESSION['Allyfolio_User_ID'])&& !empty($_SESSION['Allyfolio_User_ID']) && is_numeric($_SESSION['Allyfolio_User_ID'])){
                $this->loggedInUserId=trim($_SESSION['Allyfolio_User_ID']);    
            }
            else{
                die('CRUDOperations:__construct():: Invalid Session => '.$_SESSION['Allyfolio_User_ID'].' \t'. "\n");
            }
        }
        catch(Exception $ex){
            die('CRUDOperations:__construct():: \t'. $ex->getMessage(). "\n");   
        }
   }
     
    public function str_format()
    {
        $args = func_get_args();
        $str = mb_ereg_replace('{([0-9]+)}', '%\\1$s', array_shift($args));
        return vsprintf($str, array_values($args));
    }
       
    //check if user is authorised by comparining passed userid with session id
    /**
     * PerformPostAction::isAuthenticated()
     * 
     * @param mixed $userId
     * @return
     */
    public function isAuthenticated($userId){
        //global $loggedInUserId;
        if(isset($userId) && !empty($userId)  && is_numeric($userId) && $this->loggedInUserId==$userId){
            return true;
        }
        else{
            return false;
        }
    }
    function ifExists($tableName,$userId,$whereConditionArr, $isExistForUser=true,$conditionDefaultFor="addcluster"){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            if($this->pdodbobj!=null){// check if connection object is set to connection
                $whereConditions=array();
                if($conditionDefaultFor=="addcluster"){
                    if($isExistForUser){
                        $whereConditions=array_merge($whereConditionArr,array("Created_By_ID"=>$userId));
                        //$whereConditions=array("Cluster_Name"=>$valueToCheck,"Created_By_ID"=>$userId);    
                    }
                    //else{
                      //  $whereConditions=array("Cluster_Name"=>$valueToCheck);
                    //}
                }
                else{
                    $whereConditions=$whereConditionArr;
                }
                
                $isExist= $this->pdodbobj->checkExistance($tableName,$whereConditions);
                //echo "tablename: ".$tableName;
                //print_r($whereConditions);
                //echo "isexist: ".$isExist;
                //die();
                if($isExist==1)
                {
                     return true;     
                }
                else{
                    return false;
                }
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
                return $actionRes;
            }
        }
        catch(Exception $ex){
            $actionRes=$postAction->Failure;
            $actionRes["msg"]="Exception occurred while creating cluster";
            return $actionRes;
        }
    }
     
    function AddCluster($clustername,$userId){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $responseResult=$this->ifExists("af_clusters",$userId,array("Cluster_Name"=>$clustername),true);
                    if(is_bool($responseResult) && $responseResult==false){
                        $insertdataarr=array(
                                     "Created_By_ID"=>$userId,
                                     "Cluster_Name"=>$clustername,
                                     "Status"=>1
                                     );
                        $this->pdodbobj->insertData("af_clusters",$insertdataarr);
                        if($this->pdodbobj->affectedrows>0)
                        {
                             $this->lastInsertedID= $this->pdodbobj->last_id();
                             $actionRes=$postAction->Success;     
                             $actionRes["msg"]="Cluster added successfully";
                        }
                        else{
                            $actionRes=$postAction->Failure;
                            $actionRes["msg"]="Cluster not added";
                        }
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Cluster already exists";
                    }
                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    
    function AddClusterFriend($clusterid,$userId,$friendsArr){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    foreach($friendsArr as $friendid){
                        $verifyIfUserexistCond=array("Cluster_ID"=>$clusterid,"Friend_ID"=>$friendid,"Added_By_User_ID"=>$userId);
                        $responseResult=$this->ifExists("af_cluster_friends",$userId,$verifyIfUserexistCond,true,"addclusterfriend");
                        if(is_bool($responseResult) && $responseResult==false){
                            $insertdataarr=array(
                                         "Cluster_ID"=>$clusterid,
                                         "Friend_ID"=>$friendid,
                                         "Added_By_User_ID"=>$userId,
                                         "Status"=>1
                                         );
                            $this->pdodbobj->insertData("af_cluster_friends",$insertdataarr);
                            if($this->pdodbobj->affectedrows>0)
                            {
                                 $this->lastInsertedID= $this->pdodbobj->last_id();
                                 $actionRes=$postAction->Success;     
                                 $actionRes["msg"]="friend added successfully";
                            }
                            else{
                                $actionRes=$postAction->Failure;
                                $friendsnotadded.=$friendid.",";
                                $actionRes["msg"]="friend not added";
                            }
                        }
                        else{
                            $actionRes["err_code"]=202;
                            $alreadyAddedFriends.=$friendid.",";
                            $actionRes["msg"]="friend already exists";
                        }
                    }
                    
                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    
    function RemoveClusterFriend($clusterid,$userId,$friendsArr){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    foreach($friendsArr as $friendid){
                        $verifyIfUserexistCond=array("Cluster_ID"=>$clusterid,"Friend_ID"=>$friendid,"Added_By_User_ID"=>$userId);
                        $responseResult=$this->ifExists("af_cluster_friends",$userId,$verifyIfUserexistCond,true,"addclusterfriend");
                        if(is_bool($responseResult) && $responseResult==true){
                            $deletedataarr=array(
                                         "Cluster_ID"=>$clusterid,
                                         "Friend_ID"=>$friendid,
                                         "Added_By_User_ID"=>$userId
                                         );
                            $this->pdodbobj->deleteData("af_cluster_friends",$deletedataarr);
                            if($this->pdodbobj->affectedrows>0)
                            {
                                 $this->lastInsertedID= $this->pdodbobj->last_id();
                                 $actionRes=$postAction->Success;     
                                 $actionRes["msg"]="friend removed successfully";
                            }
                            else{
                                $actionRes=$postAction->Failure;
                                $friendsnotadded.=$friendid.",";
                                $actionRes["msg"]="friend not removed";
                            }
                        }
                        else{
                            $actionRes["err_code"]=400;
                            $alreadyAddedFriends.=$friendid.",";
                            $actionRes["msg"]="friend not exists";
                        }
                    }
                    
                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function MarkAsReadNotification($idtomark,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array( "Status"=>2 );
                    $this->pdodbobj->updateData("af_mine_notifications",$updatedataarr,array("NotificationID"=>$idtomark));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Notification marked as read";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Notification not marked";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function unFollowSourceOwner($idtounfollow,$userId){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            $followtypeid=1;
            if(CRUDOperations::isAuthenticated($userId)){
                $sourceType="PROFILE";
                    $sourceid=0;
                    switch($followtypeid){
                        case 1:
                            $sourceType='PROFILE';
                            break;
                        case 2:
                            $sourceType='PAGE';
                            break;
                        case 3:
                            $sourceType='TECHNOPATH';
                            break;
                        case 4:
                            $sourceType='BLOG';
                            break;
                    }
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection                    
                    $verifyIfUserexistCond=array("ID_To_Follow"=>$idtounfollow,"User_ID"=>$userId);
                    $responseResult=$this->ifExists("af_source_followers",$userId,$verifyIfUserexistCond,true,"follow_sources");
                    if(is_bool($responseResult) && $responseResult==true){
                        $deletedataarr=array(
                                     "ID_To_Follow"=>$idtounfollow,
                                     "User_ID"=>$userId
                                     );
                        $this->pdodbobj->deleteData("af_source_followers",$deletedataarr);
                        if($this->pdodbobj->affectedrows>0)
                        {
                             $this->lastInsertedID= $this->pdodbobj->last_id();
                             $actionRes=$postAction->Success;     
                             $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." has beed removed from your follower list";
                        }
                        else{
                            $actionRes=$postAction->Failure;
                            $friendsnotadded.=$friendid.",";
                            $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." is not removed from your follower list";
                        }
                    }
                    else{
                        $actionRes["err_code"]=202;
                        $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." is already removed from your follower list";
                    }
                    
                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function FollowSourceOwner($idtofollow,$userId,$followtypeid){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $sourceType="PROFILE";
                    $sourceid=0;
                    switch($followtypeid){
                        case 1:
                            $sourceType='PROFILE';
                            break;
                        case 2:
                            $sourceType='PAGE';
                            break;
                        case 3:
                            $sourceType='TECHNOPATH';
                            break;
                        case 4:
                            $sourceType='BLOG';
                            break;
                    }
                    $verifyIfUserexistCond=array("Source_Type"=>$sourceType,"ID_To_Follow"=>$idtofollow,"User_ID"=>$userId);
                    $responseResult=$this->ifExists("af_source_followers",$userId,$verifyIfUserexistCond,true,"follow_sources");
                    if(is_bool($responseResult) && $responseResult==false){
                        $insertdataarr=array(
                                     "Source_ID"=>$sourceid,
                                     "Source_Type"=>$sourceType,
                                     "ID_To_Follow"=>$idtofollow,
                                     "User_ID"=>$userId,
                                     "Follow_Date"=>strtotime(date("Y-m-d H:i:s"))
                                     );
                        $this->pdodbobj->insertData("af_source_followers",$insertdataarr);
                        if($this->pdodbobj->affectedrows>0)
                        {
                             $this->lastInsertedID= $this->pdodbobj->last_id();
                             $actionRes=$postAction->Success;     
                             $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." has beed added to your follower list";
                        }
                        else{
                            $actionRes=$postAction->Failure;
                            $friendsnotadded.=$friendid.",";
                            $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." is not added to your follower list";
                        }
                    }
                    else{
                        $actionRes["err_code"]=202;
                        $actionRes["msg"]="This ".ucfirst(strtolower($sourceType))." is already added to your follower list";
                    }
                    
                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function getEmotionsListJson($emotionsArr){
        $jsonData="{";
        $localdata="";
        $localJsonData="";
        $ecount=0;
        foreach($emotionsArr as $emotionL){
            $emotionL["category_name"]=getSmileyDataForSave($emotionL["category_name"],true);
            $ecount++;
            $localdata="  \"em-{1}\":{
                                \"id\": {2},
                                \"title\": \"{3}\",
                                \"imgsrc\": \"{4}\"
                            },
                        ";
            $localJsonData=$this->str_format($localdata,$ecount,$emotionL["ID"],$emotionL["category_name"],$emotionL["image_path"]);
                        
            $jsonData.=$localJsonData;
            //echo $jsonData;
        }
        if(strstr($jsonData,",")){
            $jsonData=substr(trim($jsonData),0,strlen(trim($jsonData))-1);
        }
        $jsonData.="}";
        return $jsonData;
    }
    
    
    function getEmotionsList(){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        $emotionsJsonList="";
        try{
            if($this->pdodbobj!=null){// check if connection object is set to connection
                $emrecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getEmotionslist()"));
                $emotionsJsonList=$this->getEmotionsListJson($emrecords);
                $actionRes=$postAction->Success;
                $actionRes["msg"]=$emotionsJsonList;                
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function getEmotionscatDListJson($emotionsArr){
        $jsonData="{";
        $localdata="";
        $localJsonData="";
        $ecount=0;
        foreach($emotionsArr as $emotionL){
            $emotionL["Title_Name"]=getSmileyDataForSave($emotionL["Title_Name"],true);
            $emotionL["Item_Type"]=getSmileyDataForSave($emotionL["Item_Type"],true);
            $ecount++;
            $localdata="  \"emcd-{1}-{2}\":{
                                \"id\": {2},
                                \"title\": \"{3}\",
                                \"imgsrc\": \"{4}\",
                                \"itemtype\":\"{5}\",
                                \"likes\":\"{6}\",
                                \"year\":\"{7}\"
                            },
                        ";
            $localJsonData=$this->str_format($localdata,$emotionL["Category_Id"],$emotionL["ID"],$emotionL["Title_Name"],$emotionL["Img_Path"],$emotionL["Item_Type"],$emotionL["Likes"],$emotionL["Item_publish_Year"]);
                        
            $jsonData.=$localJsonData;
            //echo $jsonData;
        }
        if(strstr($jsonData,",")){
            $jsonData=substr(trim($jsonData),0,strlen(trim($jsonData))-1);
        }
        $jsonData.="}";
        return $jsonData;
    }
    function getEmotionsCatDList($ecatid){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        $emotionscDJsonList="";
        try{
            if($this->pdodbobj!=null && is_numeric($ecatid)) {// check if connection object is set to connection
                $emrecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getEmotionsCatDlist(".$ecatid.")"));
                $emotionscDJsonList=$this->getEmotionscatDListJson($emrecords);
                $actionRes=$postAction->Success;
                $actionRes["msg"]=$emotionscDJsonList;                
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    public function updateUserInfo($oUserInfo,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array(   "First_Name"=>$oUserInfo->first_name,
                                            "Last_Name"=>$oUserInfo->last_name,
                                            "Full_Name"=>$oUserInfo->display_name,
                                            "Email_ID"=>$oUserInfo->primary_email,
                                            //"DOB"=>strtotime($oUserInfo->user_dob),
                                            "Secondary_Email_ID"=>$oUserInfo->recovery_email,
                                            "Password"=>md5($oUserInfo->new_password),
                                            "Address"=>$oUserInfo->user_location
                                        );
                    if(strlen($oUserInfo->new_password)<=0){
                        unset($updatedataarr["Password"]);
                    }
                                                           
                    $this->pdodbobj->updateData("af_users",$updatedataarr,array("User_ID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="User Info updated successfully";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to update user details";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    public function TemporaryDeleteAccount($userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                //get hidden clusterids from DB
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array(   "Profile_Status"=>3   );
                                                           
                    $this->pdodbobj->updateData("af_users",$updatedataarr,array("User_ID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="User account is temporary marked to remove";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to temporarily remove account";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    public function PermanentDeleteAccount($userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                //get hidden clusterids from DB
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array(   "Profile_Status"=>-1   );
                                                           
                    $this->pdodbobj->updateData("af_users",$updatedataarr,array("User_ID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="User account is removed";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to remove account";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    public function SendFeedBack($feedbackType,$feedbackMsg,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                //get hidden clusterids from DB
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array(   "Feedback_Type"=>$feedbackType,"Feedback_Message"=>$feedbackMsg,"User_ID"=>$userId );
                                                           
                    $this->pdodbobj->insertData("af_user_feedbacks",$updatedataarr);
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Feedback is sent";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to send feedback";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
        
    }
    
    public function updateUserPrivacySettings($fieldname,$settingsValue,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                //get hidden clusterids from DB
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array(   $fieldname=>$settingsValue   );
                                                           
                    $this->pdodbobj->updateData("af_user_privacy_settings",$updatedataarr,array("User_ID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="User privacy settings updated successfully";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to update user privacy settings";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    
    public function ReportPostContent($PostStatus,$postId,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                //get hidden clusterids from DB
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    if($PostStatus->IsHidden==0){
                        $updatedataarr=array("Report_Type"=>$PostStatus->ReportAbuse,"Report_Abuse_IDs"=>$PostStatus->ReportAbusetypes,"User_ID"=>$userId,"Post_ID"=>$postId,"Status"=>$PostStatus->IsHidden );
                    }
                    else{
                        $updatedataarr=array("Report_Type"=>$PostStatus->ReportAbuse,"Report_Abuse_IDs"=>$PostStatus->ReportAbusetypes,"User_ID"=>$userId,"Post_ID"=>$postId,"Status"=>$PostStatus->IsHidden );    
                    }
                    
                    $responseResult=$this->ifExists("af_friends_posts_settings",$userId,
                                                            array(
                                                                 "User_ID"=>$userId,
                                                                 "Post_ID"=>$postId                                                        
                                                            ),false,"");
                                                            
                    if($responseResult==false){                                
                        $this->pdodbobj->insertData("af_friends_posts_settings",$updatedataarr);
                    }
                    else{
                        $this->pdodbobj->updateData("af_friends_posts_settings",$updatedataarr,array("User_ID"=>$userId,"Post_ID"=>$postId));
                    }
                    
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Post is reported";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Failed to report post";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
        
    }   
    
    function notifyMyFriendsBirthdays($userId){        
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            if(CRUDOperations::isAuthenticated($userId)){
                if($this->pdodbobj!=null && is_numeric($userId)) {// check if connection object is set to connection
                    $frndsrecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getBirthdayAlerts(".$userId.")"));
                    if(is_array($frndsrecords) && count($frndsrecords)>0){
                        foreach($frndsrecords as $friendrec){
                            $responseResult=$this->ifExists("af_mine_notifications",$userId,
                                                            array(
                                                                 "UserID"=>$userId,
                                                                 "NotifierID"=>$friendrec["User_ID"],
                                                                 "ActivityType"=>"birthday_alerts"                                                        
                                                            ),false);
                                                            
                            if(is_bool($responseResult) && $responseResult==false){
                                //echo "inside insertion";
                                //print_r($friendrec);
                                //echo $friendrec["dob"];
                                $insertdataarr=array(
                                         "UserID"=>$userId,
                                         "NotifierID"=>$friendrec["User_ID"],
                                         "ActivityType"=>"birthday_alerts",
                                         "Affected_Table"=>"af_users",
                                         "Unique_Field_Name"=>"User_ID",
                                         "Unique_Field_Value"=>$friendrec["User_ID"],
                                         "nevent_date"=>date("Y-m-d",$friendrec["dob"]),
                                         "Status"=>1
                                         );
                                         //print_r($insertdataarr);
                                $this->pdodbobj->insertData("af_mine_notifications",$insertdataarr);
                                if($this->pdodbobj->affectedrows>0)
                                {
                                    //echo "inserted";
                                    $actionRes=$postAction->Success;    
                                }
                            }  
                            //echo "<pre>".$userId;
//                            echo ($responseResult)?"true":"false";
//                            print_r($friendrec);
//                            echo "</pre>";
//                            die("testing...");    
                        }
                    }
                    
                                    
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
            }else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function MarkAsReadAlerts($alerttypetomark,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array( "Status"=>2 );
                    $this->pdodbobj->updateData("af_mine_notifications",$updatedataarr,array("ActivityType"=>$alerttypetomark,"UserID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Alerts marked as read";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Alerts not marked";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function changePostLocation($idtoeditpost,$postlocation,$userId){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            $followtypeid=1;
            if(CRUDOperations::isAuthenticated($userId)){                
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection                    
                    $updatedataarr=array( "post_location"=>$postlocation );
                    $this->pdodbobj->updateData("af_user_posts_activities",$updatedataarr,array("Post_ID"=>$idtoeditpost,"User_ID"=>$userId));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Post location is modified";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="post location is notmodified yet";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }  
    
    function getMyNotificationPopup($userID,$nMaxNotifyIDRead){
        //echo ("Parameters: postcontent:".$postcontent."\t Userid:".$userId."\t postType:".$postType."\t privacyType: ".$privacyType);
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        $nrecords=array();
        try{
            if($this->pdodbobj!=null){// check if connection object is set to connection
                $nrecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getMyNotificationsPopup(".$userID.",".$nMaxNotifyIDRead.")"));
                $actionRes=$postAction->Success;
                $actionRes["msg"]=$nrecords;                
            }
            else{
                $actionRes=$postAction->Failure;
                $actionRes["msg"]="Unable to connect to db";
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }
    
    function MarkAsReadActivity($idtomark,$userId){
        $postAction= new PostAction();
        $actionRes = $postAction->Failure;
        try{
            //echo "<br>-->  doing stuff-->";
            //do your stuff
            $alreadyAddedFriends="";
            $friendsnotadded="";
            if(CRUDOperations::isAuthenticated($userId)){
                //echo "<br>-->  logged in -->";
                if($this->pdodbobj!=null){// check if connection object is set to connection
                    $updatedataarr=array( "Status"=>2 );
                    $this->pdodbobj->updateData("af_notificationwatcher",$updatedataarr,array("ID"=>$idtomark));
                    if($this->pdodbobj->affectedrows>0)
                    {
                         $actionRes=$postAction->Success;     
                         $actionRes["msg"]="Notification marked as read";
                    }
                    else{
                        $actionRes=$postAction->Failure;
                        $actionRes["msg"]="Notification not marked";
                    }
                }
                else{
                    $actionRes=$postAction->Failure;
                    $actionRes["msg"]="Unable to connect to db";
                }
//                $actionRes=$postAction->Success;
            }
            else{
                $actionRes=$postAction->Failed_For_Invalid_Session;
                //die('PerformPostAction:PostData():: Invalid UserId \t'."\n");
            }
            
        }
        catch(Exception $ex){
            $actionRes['msg']=$ex->getMessage();
            //die('PerformPostAction:PostData():: \t'. $ex->getMessage(). "\n");   
        }
        return $actionRes;
    }   
}

?>